package com.urchin.avatarhero.questview;

import java.util.ArrayList;

import android.database.Cursor;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.urchin.avatarhero.R;
import com.urchin.avatarhero.util.SQLiteHandler;
import com.urchin.avatarhero.util.Utilities;
import com.urchin.avatarhero.util.cal.CalBlob;
import com.urchin.avatarhero.util.cal.CalendarHandler;

/**********************************************************************
AvatarHero - A task management RPG Android Application
Copyright (C) 2013  Peter Clay

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
***********************************************************************/

public class FailedQuests extends QuestView {
	 
	@Override
	 protected void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
	     setContentView(R.layout.quests);
	     setTitle(R.string.app_name);	      
	     
	     refresh();
	 }
	
	public void refresh() {
		// Set the Header Text
	     TextView textViewHeader = (TextView) findViewById(R.id.questHeader);
		 textViewHeader.setText(R.string.title_activity_FailedQuests);
		 
	     // Create calendar data and get the completed quests from database
		 CalendarHandler calhandle = new CalendarHandler(this);
	     Utilities util = new Utilities(this);
	     ArrayList<CalBlob> cdata = calhandle.buildCalData(calhandle.getEvents(), calhandle.getInstances());
	     ArrayList<String> elist = new ArrayList<String>();
	     SQLiteHandler dbHandler = new SQLiteHandler(getApplicationContext());
	     
	     // Convert to a readable string
	     dbHandler.opendb();
	     Cursor rcursor = dbHandler.getAllFailedQuests();		// Returns _ID, Calendar ID, Event ID, Instance ID
	     
		rcursor.moveToFirst();	    
	    for (int i = 0; i < rcursor.getCount(); i++) {
	    	for(int j=0;j<cdata.size();j++) {
	    		if(String.valueOf(cdata.get(j).getInstanceid()).equals(rcursor.getString(2)) && String.valueOf(cdata.get(j).getEventid()).equals(rcursor.getString(1))) {		// Event and Instance match
	    			elist.add(util.convertToStrPattern(cdata.get(j), "caldisp",dbHandler));		// Convert to the calendar display pattern
	    			break;
	    		}
		    }
		    rcursor.moveToNext();
	    }
	   
	    rcursor.close();
	    dbHandler.closedb();
	   
	     // Create the adapter
	     qadapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, elist);
	     listView = (ListView) findViewById(R.id.questButtons);
	     listView.setAdapter(qadapter);		
	}
}
